package com.twu.biblioteca;

import java.util.Scanner;

public class BangalorePublicLibrary {

    private static LibraryMenu libraryMenu;
    private BooksRepository booksRepository;
    private MoviesRepository moviesRepository;
    private UsersRepository usersRepository;
    private static boolean isUserLoggedIn=false;
    private User loginUser;

    BangalorePublicLibrary(){
        if(isUserLoggedIn)
            libraryMenu=LibraryMenu.postLoginCreateMenu();
        else
            libraryMenu=LibraryMenu.preLoginCreateMenu();
        booksRepository=new BooksRepository();
        moviesRepository=new MoviesRepository();
        usersRepository=new UsersRepository();
    }

    public String welcomeCustomer(){
        String welcomeMessage="Welcome to Bibleotica";
        System.out.println(welcomeMessage);
        return welcomeMessage;
    }

    public int selectOption(){
        int choice=0;
        System.out.println("\nPlease Enter the Appropriate Choice:");
        Scanner in = new Scanner(System.in);
        choice = in.nextInt();
        return choice;
    }

    public String preLoginProcessUserInput(int choice){
        String displayString=null;
        switch(choice){
            case 1:
                displayString=booksRepository.displayRepository();
                System.out.println(displayString);
                break;

            case 2:
                displayString=moviesRepository.displayRepository();
                System.out.println(displayString);
                break;

            case 3:
                System.out.println("\nEnter username:");
                Scanner in = new Scanner(System.in);
                String username= in.nextLine();
                System.out.println("\nEnter password:");
                String password=in.nextLine();
                if((loginUser=usersRepository.checkUserDetails(username,password))!=null){
                    isUserLoggedIn=true;
                    libraryMenu=LibraryMenu.postLoginCreateMenu();
                    System.out.println("Login Successful");
                }
                else{
                    System.out.println("Incorrect username/password");
                }
                break;

            case 4:
                displayString="\nPlease talk to a Librarian. Thank you.";
                System.out.println(displayString);
                break;


        }
        return displayString;
    }

    public String postLoginProcessUserInput(int choice){
        String displayString=null;
        switch(choice){
            case 1:
                displayString=booksRepository.displayRepository();
                System.out.println(displayString);
                break;

            case 2:
                System.out.println("\nPlease Enter the Name Of The book to Reserve:");
                Scanner input = new Scanner(System.in);
                String bookName= input.nextLine();
                displayString=booksRepository.reserveBook(bookName);
                System.out.println(displayString);
                break;

            case 3:
                displayString=moviesRepository.displayRepository();
                System.out.println(displayString);
                break;

            case 4:
                System.out.println("Membership Details: ");
                displayString=loginUser.displayUserDetails();
                System.out.println(displayString);
                break;

            case 5:
                isUserLoggedIn=false;
                libraryMenu=LibraryMenu.preLoginCreateMenu();
                System.out.println("User Successfully Logged Out");
                break;

        }
        return displayString;
    }

    public boolean isMenuOptionValid(int choice){
        if(isUserLoggedIn) {
            if(choice<1 || choice>6)
                return false;
            return true;
        }
        else{
            if(choice<1 || choice>5)
                return false;
            return true;
        }
    }

    public static void main(String[] args) {
        BangalorePublicLibrary bangalorePublicLibrary=new BangalorePublicLibrary();
        bangalorePublicLibrary.welcomeCustomer();
        while(true) {
            libraryMenu.displayMenu();
            int choice;
            choice=bangalorePublicLibrary.selectOption();

            if(!bangalorePublicLibrary.isMenuOptionValid(choice)){
                System.out.println("Select a valid option!!");
                libraryMenu.displayMenu();
            }
            else if(isUserLoggedIn==false && choice==5){
                break;
            }
            else if(isUserLoggedIn==true && choice==6){
                break;
            }
            else{
                if(isUserLoggedIn)
                    bangalorePublicLibrary.postLoginProcessUserInput(choice);
                else
                    bangalorePublicLibrary.preLoginProcessUserInput(choice);
                System.out.println("\nPress Enter to Go To The Menu");
                Scanner in = new Scanner(System.in);
                in.nextLine();
            }

        }
    }
}
